<template>
  <div>
    <el-dialog :title="title" v-model="dialogFormVisible" size="samll" :before-close="closeDialog">
      <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="demo-form">
        <el-form-item label="原密码" prop="oldPwd">
          <el-input type="password" v-model="form.oldPwd"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input type="password" v-model="form.newPwd"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="再输入新密码" prop="repPwd">
          <el-input type="password" v-model="form.repPwd"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item class="button-row">
          <el-button type="primary" @click.native.prevent="handleSubmit">修改密码</el-button>
          <el-button @click.native.prevent="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'
  var model = {oldPwd: '', newPwd: '', repPwd: ''};
  export default {
    data () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.from.newPwd !== '') {
            this.$refs.ruleForm2.validateField('repPwd');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.oldPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        title: '修改密码', form: Object.assign({}, model),
        rules: {
          newPwd: [
            {required: true, message: '请输入新密码'},
            {min: 6, max: 20, message: '长度在 6到 20 个字符'}
          ],
          oldPwd: [
            {required: true, message: '请输入原密码'},
            {min: 6, max: 20, message: '长度在 6到 20 个字符'}
          ],
          repPwd: [
            {required: true, message: '再输入一次新密码'},
            {min: 6, max: 20, message: '长度在 6到 20 个字符'},
          ]
        }
      }
    },
    props: {
      dialogFormVisible: {
        type: Boolean,
        default: false
      }
    },
    computed: {
    },
    methods: {
      handleReset () {
        this.$refs.form.resetFields();
      }, closeDialog(){
        this.$parent.closeDialog();
      },
      handleSubmit (ev) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            http.postData(vm, "/sys/account/modifyPwd", vm.form, function (resp) {
              var ret = resp.body;
              if (ret.code === '111') {
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
                vm.handleReset();
                vm.closeDialog();
              } else {
                vm.$notify.error({title: '操作失败', message: ret.msg});
              }
            });
          } else {
            return false
          }
        })
      }
    }
  }
</script>
